package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.RescueData;
import com.ruoyi.system.domain.TipsData;
import com.ruoyi.system.service.IOpenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ElevatorInfoMapper;
import com.ruoyi.system.domain.ElevatorInfo;
import com.ruoyi.system.service.IElevatorInfoService;

import javax.annotation.Resource;

/**
 * 电梯信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-17
 */
@Service
public class ElevatorInfoServiceImpl implements IElevatorInfoService 
{
    @Autowired
    private ElevatorInfoMapper elevatorInfoMapper;
    @Resource
    private IOpenService iOpenService;

    /**
     * 查询电梯信息
     * 
     * @param elevatorId 电梯信息主键
     * @return 电梯信息
     */
    @Override
    public ElevatorInfo selectElevatorInfoByElevatorId(Long elevatorId)
    {
        return elevatorInfoMapper.selectElevatorInfoByElevatorId(elevatorId);
    }

    /**
     * 查询电梯信息列表
     * 
     * @param elevatorInfo 电梯信息
     * @return 电梯信息
     */
    @Override
    public List<ElevatorInfo> selectElevatorInfoList(ElevatorInfo elevatorInfo)
    {
        if(elevatorInfo.getIsSet()!=null&&elevatorInfo.getIsSet().equals("Y")){
            elevatorInfo.setIsNfcid(1);
        }
        if(elevatorInfo.getIsSet()!=null&&elevatorInfo.getIsSet().equals("N")){
            elevatorInfo.setIsNfcid(0);
        }
        return elevatorInfoMapper.selectElevatorInfoList(elevatorInfo);
    }

    /**
     * 新增电梯信息
     * 
     * @param elevatorInfo 电梯信息
     * @return 结果
     */
    @Override
    public int insertElevatorInfo(ElevatorInfo elevatorInfo)
    {
        return elevatorInfoMapper.insertElevatorInfo(elevatorInfo);
    }

    /**
     * 修改电梯信息
     * 
     * @param elevatorInfo 电梯信息
     * @return 结果
     */
    @Override
    public int updateElevatorInfo(ElevatorInfo elevatorInfo)
    {

        RescueData rescueData=new RescueData();
        rescueData.setSBZCDM(elevatorInfo.getSBZCDM());
        rescueData.setNFCID(elevatorInfo.getNfcid());
        iOpenService.setRescueData(rescueData);
         return  elevatorInfoMapper.updateElevatorInfo(elevatorInfo);
    }

    @Override
    public void hisElevatorInfo() {
        List<ElevatorInfo> list=elevatorInfoMapper.selectElevatorInfoListByNoList();
        list.forEach(x->{
            RescueData rescueData=new RescueData();
            rescueData.setSBZCDM(x.getSBZCDM());
            rescueData.setNFCID(x.getNfcid());
            iOpenService.setRescueData(rescueData);
        });
    }

    /**
     * 批量删除电梯信息
     * 
     * @param elevatorIds 需要删除的电梯信息主键
     * @return 结果
     */
    @Override
    public int deleteElevatorInfoByElevatorIds(Long[] elevatorIds)
    {
        return elevatorInfoMapper.deleteElevatorInfoByElevatorIds(elevatorIds);
    }
    @Override
    public  List<ElevatorInfo> selectElevatorInfoByElevatorIds(Long[] elevatorIds){
        return elevatorInfoMapper.selectElevatorInfoByElevatorIds(elevatorIds);
    }

    /**
     * 删除电梯信息信息
     * 
     * @param elevatorId 电梯信息主键
     * @return 结果
     */
    @Override
    public int deleteElevatorInfoByElevatorId(Long elevatorId)
    {
        return elevatorInfoMapper.deleteElevatorInfoByElevatorId(elevatorId);
    }

    @Override
    public AjaxResult sync() {
        List<TipsData> tipsDataList = iOpenService.getRescueData();
        List<ElevatorInfo> insertListData=new  ArrayList();
        List<ElevatorInfo> listData=elevatorInfoMapper.selectElevatorInfoList(new ElevatorInfo());
        tipsDataList.forEach(x->{
            //验证是否存在
            List<ElevatorInfo> elevatorInfo1=listData.stream().filter(y->y.getSBZCDM().equals(x.getSBZCDM())).collect(Collectors.toList());
            if(elevatorInfo1!=null&&elevatorInfo1.size()>0) {
                ElevatorInfo temp=elevatorInfo1.get(0);
                //elevatorInfoMapper.updateElevatorInfo(temp);
            }else{
                ElevatorInfo elevatorInfo=new ElevatorInfo();
                JSONObject resultJSON=iOpenService.getDetail(x.getSBZCDM());
                JSONArray tempArry=resultJSON.getJSONArray("data");
                JSONObject temp=tempArry.getJSONObject(0);
                elevatorInfo.setDTLX(temp.getString("DTLX"));
                elevatorInfo.setDTBH(temp.getString("DTBH"));
                elevatorInfo.setWBDWMC(temp.getString("WBDWMC"));
                elevatorInfo.setSYDWMC(temp.getString("SYDWMC"));
                elevatorInfo.setSBSZDQ(temp.getString("SBSZDQ"));
                elevatorInfo.setSBAZDD(temp.getString("SBAZDD"));
                elevatorInfo.setSBZCDM(x.getSBZCDM());
                insertListData.add(elevatorInfo);
            }
        });
        batchInsertElevatorInfo(insertListData);
        return AjaxResult.success("同步成功");
    }
    /**
     * 批量插入电梯信息
     *
     * @param elevatorInfoList 电梯信息列表
     * @return 插入成功的记录数
     */
    private int batchInsertElevatorInfo(List<ElevatorInfo> elevatorInfoList)
    {
        if (StringUtils.isNull(elevatorInfoList) || elevatorInfoList.isEmpty())
        {
            return 1;
        }
        return elevatorInfoMapper.batchInsertElevatorInfo(elevatorInfoList);
    }

    @Override
    public String importData(List<ElevatorInfo> elevatorInfoList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(elevatorInfoList) || elevatorInfoList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ElevatorInfo elevatorInfo : elevatorInfoList)
        {
            try
            {
                //验证是否存在
                ElevatorInfo elevatorInfo1=elevatorInfoMapper.selectElevatorInfoByElevatorNo(elevatorInfo.getSBZCDM());
                if(elevatorInfo1!=null)
                {
                    elevatorInfo.setElevatorId(elevatorInfo1.getElevatorId());
                    elevatorInfoMapper.updateElevatorInfo(elevatorInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "编号： " + elevatorInfo.getSBZCDM() + " 更新成功");
                }else{
                    elevatorInfo.setSBSZDQ(elevatorInfo.getProvince()+""+elevatorInfo.getCity());
                    // 验证是否存在这个用户
                    elevatorInfoMapper.insertElevatorInfo(elevatorInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "编号： " + elevatorInfo.getSBZCDM() + " 导入成功");
                }

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + " 导入失败："+elevatorInfo.getSBZCDM();
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
